package com.play.ballen.me.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.home.entity.ArticleInfo
import com.play.ballen.me.entity.MoneyLogInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger

open class MoneyLogVM : BaseViewModel() {

    protected val model = ApiRepository()

    var page = 1
    private val pageSize = 20

    val listMoneyLog = MutableLiveData<List<MoneyLogInfo>?>()
    val actionGetMoneyLogFailure = SingleLiveEvent<String>()

    fun moneyLog(type: String, money_type: String) {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        if (type.isNotEmpty()) {
            map["type"] = type
        }
        if (money_type.isNotEmpty()) {
            map["money_type"] = money_type
        }

        model.moneyLog(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                listMoneyLog.value = data
                Logger.i("MoneyLogVM.moneyLog success list = $data")
            }, { throwable ->
                actionGetMoneyLogFailure.value = throwable.message
                Logger.i("MoneyLogVM.moneyLog error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}